//图书管理模块
//图书管理模块
import java.sql.*;
import java.util.Scanner;

public class BookManager {
    private Scanner scanner = new Scanner(System.in);

    public void menu() {
        while (true) {
            System.out.println("\n--- 图书管理 ---");
            System.out.println("1. 添加图书");
            System.out.println("2. 删除图书");
            System.out.println("3. 查看所有图书");
            System.out.println("4. 返回主菜单");
            int choice = getIntInput("请选择操作：");

            switch (choice) {
                case 1:
                    addBook();
                    break;
                case 2:
                    deleteBook();
                    break;
                case 3:
                    listBooks();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    private void addBook() {
        String title = getStringInput("请输入图书标题：");
        String author = getStringInput("请输入图书作者：");

        String checkSql = "SELECT book_id FROM books WHERE title = ? AND author = ?";
        String insertSql = "INSERT INTO books (title, author, status) VALUES (?, ?, 'available')";

        try (Connection conn = DBHelper.getConnection()) {
            // 验证图书是否存在
            try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
                checkStmt.setString(1, title);
                checkStmt.setString(2, author);
                ResultSet rs = checkStmt.executeQuery();
                if (rs.next()) {
                    System.out.println("该图书已存在。");
                    return;
                }
            }

            // 添加图书
            try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
                insertStmt.setString(1, title);
                insertStmt.setString(2, author);
                insertStmt.executeUpdate();
                System.out.println("图书添加成功。");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加图书失败： " + e.getMessage());
        }
    }

    private void deleteBook() {
        int bookId = getIntInput("请输入图书ID：");

        String checkSql = "SELECT book_id FROM books WHERE book_id = ?";
        String deleteSql = "DELETE FROM books WHERE book_id = ?";

        try (Connection conn = DBHelper.getConnection()) {
            // 验证图书是否存在
            try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
                checkStmt.setInt(1, bookId);
                ResultSet rs = checkStmt.executeQuery();
                if (!rs.next()) {
                    System.out.println("图书ID不存在。");
                    return;
                }
            }

            // 删除图书
            try (PreparedStatement deleteStmt = conn.prepareStatement(deleteSql)) {
                deleteStmt.setInt(1, bookId);
                deleteStmt.executeUpdate();
                System.out.println("图书删除成功。");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除图书失败： " + e.getMessage());
        }
    }

    private void listBooks() {
        String sql = "SELECT book_id, title, author, status FROM books";

        try (Connection conn = DBHelper.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            System.out.println("\n图书ID\t标题\t作者\t状态");
            while (rs.next()) {
                System.out.printf("%d\t%s\t%s\t%s\n",
                        rs.getInt("book_id"),
                        rs.getString("title"),
                        rs.getString("author"),
                        rs.getString("status"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询图书失败： " + e.getMessage());
        }
    }

    private int getIntInput(String prompt) {
        while (true) {
            System.out.print(prompt);
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入合法数字！");
            }
        }
    }

    private String getStringInput(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine().trim();
    }
}

